package com.example.user.controller;

import com.example.user.entity.User;
import com.example.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 8:51 2019/4/3
 */
@Controller
public class UserController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @PostMapping("/user/login")
    @ResponseBody
    public  String login(@RequestBody User user,HttpSession session){
        User currentUser=userService.login(user);
        if (currentUser==null){
            return "login_fail";
        }else {
            session.setAttribute("userInfo",currentUser);
            return "login_succ";
        }
    }
    @PostMapping("/user/register")
    @ResponseBody
    public  String register(@RequestBody User user){
          userService.register(user);
           return "succ";
    }
    @PostMapping("/user/loginCheck")
    @ResponseBody
    public User loginCheck(HttpSession session){
      User user= (User) session.getAttribute("userInfo");
      return user;
    }
    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userInfo");
        return "redirect:/home";
    }
    @RequestMapping("/user/editInfo")
    @ResponseBody
    public String editInfo(HttpSession session,@RequestBody User user){
       User currentUser=userService.editInfo(user);
       if (currentUser==null){
           return "edit_fail";
       }else {
           session.removeAttribute("userInfo");
           session.setAttribute("userInfo",currentUser);
           return "edit_succ";
       }

    }

}
